//
//  MovieViewController.m
//  MyMovie
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 ljp. All rights reserved.
//

#import "MovieViewController.h"
#import "Movie.h"
#import "MovieCell.h"
#import "MoviePostView.h"

@interface MovieViewController ()<UITableViewDelegate, UITableViewDataSource>


{
    NSMutableArray *movieArray;
}
@property (strong, nonatomic) IBOutlet UITableView *listView;
@property (strong, nonatomic) IBOutlet MoviePostView*postView;

@end

@implementation MovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadJsonFile];
    [self createRightButton];

    //设定初始偏移值
    _listView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
}

- (void)loadJsonFile {

    //1找到文件地址
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"us_box" ofType:@"json"];
    //2读取文件数据
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    //3解析文件内容
    //以前json读取方式:JSONKit  TouchJSON  SBJSON
    //用id result 接受解析后的内容,因为不知道是字典还是数组,确定之后可以用具体的类
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data
                                                            options:NSJSONReadingMutableLeaves
                                                              error:nil];

    movieArray = [[NSMutableArray alloc] init];
    for (NSDictionary *d in jsonDic[@"subjects"]) {

        Movie *movie = [[Movie alloc] initWithDictionary:d];
        [movieArray addObject:movie];
    }
    _postView.movieArray = movieArray;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 120;
}

#pragma mark 表视图实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return movieArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MovieCell"];

    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MovieCell" owner:nil options:nil] lastObject];
    }
    cell.movie = movieArray[indexPath.row];
    return cell;
}

- (void)createRightButton {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 49, 20);
    [button setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateSelected];

    [button addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = buttonItem;


}

- (void)rightButtonAction:(UIButton *)button {


    UIViewAnimationOptions option = button.selected ? UIViewAnimationOptionTransitionFlipFromLeft :
    UIViewAnimationOptionTransitionFlipFromRight;

    [UIView transitionWithView:button
                      duration:0.4
                       options:option
                    animations:^{

                        button.selected = !button.selected;
                    }
                    completion:nil];

    [UIView transitionWithView:self.view
                      duration:0.4
                       options:option
                    animations:^{

                        _listView.hidden = button.selected;
                        _postView.hidden = !button.selected;
                    }
                    completion:nil];
}

@end
